package com.example.certificate.config;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.example.certificate.utils.JsonUtil;
import org.apache.ibatis.reflection.MetaObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Date;

@Component
public class MetaObjectHandleConfig implements MetaObjectHandler {

    private static final Logger log = LoggerFactory.getLogger(MetaObjectHandleConfig.class);

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ....");

        Object delFlag = getFieldValByName("delFlag", metaObject);
        Object useFlag = getFieldValByName("useFlag", metaObject);
        Object createId = getFieldValByName("createId", metaObject);
        Object createTime = getFieldValByName("createTime", metaObject);

        if (delFlag == null) {
            setFieldValByName("delFlag", 1, metaObject);
        }
        if (useFlag == null) {
            setFieldValByName("useFlag", 1, metaObject);
        }
        if (createId == null) {
            Long id = this.getCurrentUser();
            setFieldValByName("createId", StringUtils.isEmpty(id) ? 0L : id, metaObject);
        }
        if (createTime == null) {
            setFieldValByName("createTime", new Date(), metaObject);
        }

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ....");
        Object updateId = getFieldValByName("updateId", metaObject);
        Object updateTime = getFieldValByName("updateTime", metaObject);
        if (updateId == null) {
            Long id = this.getCurrentUser();
            setFieldValByName("updateId", StringUtils.isEmpty(id) ? 0L : id, metaObject);
        }
        if (updateTime == null) {
            setFieldValByName("updateTime", new Date(), metaObject);
        }
    }

    /**
     * @Author Hao
     * @Description  获取当前登录用户
     * @return java.lang.String
     **/
    public Long getCurrentUser() {
        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        Object object = request.getAttribute("CurrentUser");
        if (object==null){
            return null;
        }
        String userStr = JsonUtil.Object2JsonString(object);
        JSONObject jsonObject = JSONObject.parseObject(userStr);
        return jsonObject.getLong("id");
    }

    /**
     * @Author Hao
     * @Description  返回前端错误信息
     * @return void
     **/
    public static void sendJsonMessage(HttpServletResponse response, Object obj) throws Exception {
        response.setContentType("application/json; charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.print(JSONObject.toJSONString(obj, SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteDateUseDateFormat));
        writer.close();
        response.flushBuffer();
    }
}
